// pages/secret/index.js
const API = require("../../common/constant.js");
const util = require("../../utils/util");
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tagList: [
      {
        tagName: "我的标签",
        isClick: false,
        tagKey: "userLabel"
      },
      {
        tagName: "我的评价",
        isClick: false,
        tagKey: 'userReply'
      },
      {
        tagName: "我的认证视频",
        isClick: false,
        tagKey: 'userVideo'
      },
      {
        tagName: "我的相册",
        isClick: false,
        tagKey: 'userPhoto'
      },
      {
        tagName: "我的兴趣爱好",
        isClick: false,
        tagKey: 'userHobby'
      }
    ]
  },
  clickIcon (e) {
    let index = e.currentTarget.dataset.index;
    let item = "tagList[" + index + "].isClick";
    this.setData({
      [item]: !this.data.tagList[index]['isClick']
    })
    let name = '';
    let param = { };
    let tagList = this.data.tagList
    for(let i in tagList) {
      name = tagList[i].tagKey;
      param[name] = tagList[i].isClick ? 'on' : 'off'
    }
    this.changeSecret(param);
  },
  getSecret() {
    util.httpbody({
      url: API.URL.USER.FIND_PRIVACY_INFO,
      success: res => {
        console.log(res)
        if (res.msg == '未查询到隐私设置！') return
        let bussData = res.data.bussData
        console.log(bussData)
        let tagList = this.data.tagList
        tagList[0].isClick = bussData.userLabel === 'on'? true: false;
        tagList[1].isClick = bussData.userReply === 'on' ? true : false;
        tagList[2].isClick = bussData.userVideo === 'on' ? true : false;
        tagList[3].isClick = bussData.userPhoto === 'on' ? true : false;
        tagList[4].isClick = bussData.userHobby === 'on' ? true : false;
        this.setData({
          tagList: tagList
        })
      },
      fail: err => {
        console.log('err', err)
      }
    })
  },
  changeSecret(e) {
    console.log(e)
    util.httpbody({
      url: API.URL.USER.SAVE_PRIVACY_USER,
      data: e,
      success: res => {
        console.log('res', res)
      },
      fail: err => {
        console.log('err', err)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getSecret();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})